import time

import requests
from bs4 import BeautifulSoup

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    # 'Cookie': 'acw_tc=1a0c399717236903443525451e0041fcc4d9cad5d063fc1ff2b3cbbf8ac2b2; ASPSESSIONIDCUQCDDQT=JEHANKKCFEHPIKMCMJCFGELM; __root_domain_v=.zdaye.com; _qddaz=QD.942023690345146; _qdda=3-1.10ga5x; _qddab=3-u7gbh4.lzuoq6f8; lastSE=google; __51uvsct__20L1wEeeGTFXijbh=1; __51vcke__20L1wEeeGTFXijbh=f45b95ce-a197-515f-8711-41c942562a5a; __51vuft__20L1wEeeGTFXijbh=1723690345290; Hm_lvt_dd5f60791e15b399bf200ae217689c2f=1723690345; HMACCOUNT=4FEFBD8348B1CFD2; __vtins__20L1wEeeGTFXijbh=%7B%22sid%22%3A%20%2279140dab-a8b7-5446-b129-569e5da4273a%22%2C%20%22vd%22%3A%206%2C%20%22stt%22%3A%20313854%2C%20%22dr%22%3A%207293%2C%20%22expires%22%3A%201723692459142%2C%20%22ct%22%3A%201723690659142%7D; Hm_lpvt_dd5f60791e15b399bf200ae217689c2f=1723690659',
    'Referer': 'https://www.google.com/',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
}
cookies = {
    'acw_tc': '1a0c399717236903443525451e0041fcc4d9cad5d063fc1ff2b3cbbf8ac2b2',
    'ASPSESSIONIDCUQCDDQT': 'JEHANKKCFEHPIKMCMJCFGELM',
    '__root_domain_v': '.zdaye.com',
    '_qddaz': 'QD.942023690345146',
    '_qdda': '3-1.10ga5x',
    '_qddab': '3-u7gbh4.lzuoq6f8',
    'lastSE': 'google',
    '__51uvsct__20L1wEeeGTFXijbh': '1',
    '__51vcke__20L1wEeeGTFXijbh': 'f45b95ce-a197-515f-8711-41c942562a5a',
    '__51vuft__20L1wEeeGTFXijbh': '1723690345290',
    'Hm_lvt_dd5f60791e15b399bf200ae217689c2f': '1723690345',
    'HMACCOUNT': '4FEFBD8348B1CFD2',
    '__vtins__20L1wEeeGTFXijbh': '%7B%22sid%22%3A%20%2279140dab-a8b7-5446-b129-569e5da4273a%22%2C%20%22vd%22%3A%206%2C%20%22stt%22%3A%20313854%2C%20%22dr%22%3A%207293%2C%20%22expires%22%3A%201723692459142%2C%20%22ct%22%3A%201723690659142%7D',
    'Hm_lpvt_dd5f60791e15b399bf200ae217689c2f': '1723690659',
}


def getProxy():
    # result = requests.get('https://www.zdaye.com/free/', headers=headers, cookies=cookies).text
    ips = []
    retryTime = 10
    while len(ips) == 0 and retryTime > 0:
        try:
            result = requests.get('https://cn.proxy-tools.com/proxy/cn', headers=headers, cookies=cookies,
                                  timeout=5).text
            bf = BeautifulSoup(result, features="html.parser")
            trs = bf.find(name='tbody').find_all('tr')

            for tr in trs:
                tds = tr.find_all('td')
                # ip = f"{tds[0].text}:{tds[1].text}"
                ip = f"{tds[0].text}:80"
                print(tds[2].text.strip())
                if tds[2].text.strip() == 'HTTP' and requests.get('https://baidu.com', headers=headers, cookies=cookies, proxies={'http': ip, 'https': ip},
                                timeout=3).status_code == 200:
                    ips.append(ip)
        except:
            print("error retry")
        time.sleep(1)
        retryTime -= 1
    return ips

# if __name__ == '__main__':
#     getProxy()
